package com.fyt.service.Impl.fencing;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fyt.controller.utils.Code;
import com.fyt.controller.utils.Result;
import com.fyt.entity.Fencing;
import com.fyt.mapper.FencingMapper;
import com.fyt.service.fencing.FencingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fyt
 * @since 2023-12-21
 */
@Service
public class FencingServiceImpl extends ServiceImpl<FencingMapper, Fencing> implements FencingService {

    @Autowired
    private FencingMapper fencingMapper;
    @Override
    public Result addFencing(Map<String, String> map) {
        String enclosureId = map.get("enclosureId");
        String FencingName = map.get("FencingName");
        if(FencingName.length()>20){
            return new Result(null,Code.save_err,"名称不能超过20");
        }
        QueryWrapper<Fencing> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",FencingName);
        queryWrapper.eq("e_id",enclosureId);
        List<Fencing> fencings = fencingMapper.selectList(queryWrapper);
        if(!fencings.isEmpty()){
            return new Result(null,Code.save_err,"该围栏名称此圈舍中已经存在");
        }
        Fencing fencing = new Fencing();
        fencing.setName(FencingName);
        fencing.setEnable("0");
        fencing.setEId(Integer.parseInt(enclosureId));
        fencing.setInventory(10);
        int i = fencingMapper.insert(fencing);
        return new Result(null,i>0? Code.save_ok:Code.save_err,i>0?"success":"error");
    }

}
